﻿@page "/login"
@using BlazorProject.Service;
@using System.ComponentModel.DataAnnotations;
@using Microsoft.AspNetCore.Components.Authorization
@inject AuthenticationStateProvider AuthenticationStateProvider
@inject NavigationManager NavigationManager
@inject ISysService sysService

<div class="layui-card">
    <div class="layadmin-user-login-main">
        <div class="layadmin-user-login-box layadmin-user-login-header">
            <h2>后台管理系统</h2>
        </div>
        <EditForm Model="@loginmodel" OnValidSubmit="@onLogin">
            <DataAnnotationsValidator />
            <ValidationSummary />

            <InputText id="username" @bind-Value="loginmodel.UserName" />

            <InputText id="password" @bind-Value="loginmodel.Password" />

            <InputText id="vercode" @bind-Value="loginmodel.VerCode" />

            <button type="submit">登录</button>
        </EditForm>
    </div>
</div>
@code {
    public class LoginModel
    {
        [StringLength(10, ErrorMessage = "UserName is too long.")]
        [Required(AllowEmptyStrings = false, ErrorMessage = "用户名不能为空！")]
        public string UserName { get; set; }
        [StringLength(20, ErrorMessage = "Password is too long.")]
        [Required(AllowEmptyStrings = false, ErrorMessage = "密码不能为空！")]
        public string Password { get; set; }
        [Required(AllowEmptyStrings = false, ErrorMessage = "验证码不能为空！")]
        public string VerCode { get; set; }
    }

    private LoginModel loginmodel = new LoginModel();

    private async Task onLogin()
    {
        if (loginmodel.UserName == "admin" && loginmodel.Password == "123456" && loginmodel.VerCode == "123456")
        {
            //sysService.SignIn(HttpContext, loginmodel.UserName, loginmodel.Password, loginmodel.VerCode);
            NavigationManager.NavigateTo("counter");
        }
        var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
        var user = authState.User;

        if (user.Identity.IsAuthenticated)
        {
            Console.WriteLine($"{user.Identity.Name} is authenticated.");
        }
        else
        {
            Console.WriteLine("The user is NOT authenticated.");
        }
    }
}
